package com.finance.cooperate.data.base;

import com.finance.cooperate.common.utils.SpringContextUtil;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.data.feature.FeatureDefinition;
import com.finance.cooperate.data.service.AppService;
import com.finance.cooperate.data.service.ApplyService;
import com.finance.cooperate.data.service.BorrowingService;
import com.finance.cooperate.data.service.SmsService;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ReaderImpl
 * @Description TODO
 * @Author shen
 * @Date 2023/8/10 21:26
 * @Modify ...
 */
public abstract class ReaderImpl implements Reader{

    // 返回结果
    private Map<String, String> result;

    protected AppService appService;

    protected BorrowingService borrowingService;

    protected SmsService smsService;

    protected ApplyService applyService;


    public ReaderImpl() {

        this.result = new HashMap<>();

        ApplicationContext context = SpringContextUtil.getApplicationContext();


        appService = (AppService) context.getBean("appServiceImpl");
        borrowingService = (BorrowingService) context.getBean("borrowingServiceImpl");
        smsService = (SmsService) context.getBean("smsServiceImpl");
        applyService = (ApplyService) context.getBean("applyServiceImpl");

    }


    protected void put(FeatureDefinition key, Object value) {
        result.put(key.getValue(), StringUtil.obj2Feature(value));
    }

    protected void putOriginal(FeatureDefinition key, String value) {
        result.put(key.getValue(), value);
    }


    protected void put(String key, Object value) {
        result.put(key, StringUtil.obj2Feature(value));
    }

    protected String get(String key) {
        return result.getOrDefault(key, "");
    }

    protected String getOrDefault(String key, Object defaultValue) {
        return result.getOrDefault(key, String.valueOf(defaultValue));
    }

    protected String get(FeatureDefinition key) {
        return result.getOrDefault(key.getValue(), "");
    }

    protected String getOrDefault(FeatureDefinition key, Object defaultValue) {
        return result.getOrDefault(key.getValue(), String.valueOf(defaultValue));
    }

    protected Map<String, String> getResult() {
        return result;
    }

    public ReaderImpl setResult(Map<String, String> result) {
        this.result = result;
        return this;
    }

    @Override
    public abstract Map<String, String> read(String userId);

}
